import React from 'react';
import {
  FormPanel,
  MinioUploadListType,
  type FormPanelObject,
  FormPanelBtnAlign,
  FormPanelBtnDisplay,
  InputType,
} from 'lfun-bg-comp';
import { Modal } from 'antd';
import { StuGraduationSrv } from '@/service/student';

const Certification: React.FC<{ show: boolean; onChange: Function; formValue: any }> = ({
  show,
  onChange,
  formValue = {},
}) => {
  const list = [
    {
      title: '',
      labelCol: { span: 4 },
      wrapperCol: { span: 20 },
      span: 24,
      inputs: [
        {
          label: '申请学生',
          name: 'student_name',
          key: 'student_name',
          required: true,
          textRange: [0, 18],
          disabled: () => true,
        },
        {
          label: '毕业照上传',
          name: 'graduation_photo',
          key: 'graduation_photo',
          required: true,
          inputType: InputType.minioUpload,
          inputProps: {
            placeholder: '请输入相关证件上传',
            listType: MinioUploadListType.picture,
            text: '浏览',
            maxCount: 1,
            uploadSrv: '/school/v1/storage/school-upload-uri',
          },
        },
        {
          label: '备注',
          name: 'credential_notes',
          key: 'credential_notes',
          required: false,
          inputType: InputType.textarea,
          textRange: [0, 200],
        },
      ],
    },
  ];

  const onSubmit = async (formPanelObject: FormPanelObject) => {
    const values = formPanelObject.getFieldsValue();
    const params = {
      ...values,
      student_id: formValue?.student_id,
    };
    if (Array.isArray(values.graduation_photo) && values.graduation_photo.length > 0) {
      params.graduation_photo = String(values.graduation_photo[0].uid);
    }

    // 保存逻辑
    const res = await StuGraduationSrv.credential(params);
    if (res.success) {
      formPanelObject.resetFields();
      onChange(true);
    }
  };

  return (
    <>
      <Modal title="制证" width="50%" footer={[]} open={show} onCancel={() => onChange(false)}>
        <FormPanel
          btnsAlign={FormPanelBtnAlign.center}
          buttonsDisplay={FormPanelBtnDisplay.block}
          restBtnText="返回"
          submitBtnText="确定"
          values={formValue}
          onFinish={(formPanelObject: FormPanelObject) => {
            onSubmit(formPanelObject);
            return false;
          }}
          onReset={() => onChange(false)}
          template={list}
        />
      </Modal>
    </>
  );
};

export default Certification;
